package druid;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @program 0527svn
 * @description: druid加密
 * @author: zhanglu
 * @create: 2019-07-22 13:29:00
 */
public class DruidUtils {

    public static void main(String[] args) {
        try {
            String root = System.getProperty("user.dir");
            //获取key对应的value值
            String druidPath = root +"/druid.jar";
            System.out.println("druid.jar目录：" + druidPath);
            System.out.println("》》》》》请输入密码》》》》》");
            Scanner scanner = new Scanner(System.in);
            String pass = scanner.next();
            String command = "java -cp "+druidPath+" com.alibaba.druid.filter.config.ConfigTools "+pass+"";
            Process process =  Runtime.getRuntime().exec(command);
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            List<String> res = new ArrayList<String>();
            String rs = "";
            String str = "";
            int index = -1;
            while ((str = br.readLine()) != null){
                if(str.contains("privateKey") || str.contains("publicKey") || str.contains("password")){
                    index ++;
                    rs = "";
                }
                rs += str;
                res.add(index, rs);
            }
            System.out.println("》》》》》最终结果》》》》》");
            for(int i = 0; i < res.size(); i++){
                if(i == 1){
                    System.out.println("connectionProperties属性应该设置为：");
                    String s = res.get(i);
                    s = s.replace("publicKey:", "");
                    s = "config.decrypt=true;config.decrypt.key=" + s;
                    System.out.println(s);
                }else if(i == 2){
                    System.out.println("password属性应该设置为：");
                    String s = res.get(i);
                    s = s.replace("password:", "");
                    System.out.println(s);
                }
                System.out.println();
            }

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("执行异常：" + e);
        }
    }

}
